#!/usr/bin/env perl
###########################################################################
#
#   Copyright 2010 American Public Media Group
#
#   This file is part of AIR2.
#
#   AIR2 is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   AIR2 is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################

use strict;
use warnings;
use Test::More tests => 17;
use lib 'tests/search';
use Data::Dump qw( dump );
use AIR2TestUtils;
use AIR2::Config;
use AIR2Test::Source;
use AIR2Test::Project;
use AIR2Test::SrcResponseSet;
use AIR2Test::Organization;
use AIR2Test::Inquiry;
use Rose::DBx::Object::Indexed::Indexer;
use Search::Tools::XML;

my $stxml         = Search::Tools::XML->new;
my $debug         = $ENV{PERL_DEBUG} || 0;
my $TEST_USERNAME = 'ima-test-user';
my $TEST_PROJECT  = 'ima-test-project';

# pk depends on fixtures order
my $prefs = AIR2::SearchUtils::all_preference_values_by_id();
my $en_US;
for ( keys %$prefs ) {
    if ( $prefs->{$_}->ptv_value eq 'en_US' ) {
        $en_US = $prefs->{$_};
    }
}
if ( !$en_US ) {
    die "Can't find preference type for 'en_US'";
}

#dump( $en_US->column_value_pairs );

# reuse this
my $xml;

$Rose::DB::Object::Debug          = $debug;
$Rose::DB::Object::Manager::Debug = $debug;

###################################
##           source
###################################

ok( my $source = AIR2Test::Source->new( src_username => $TEST_USERNAME ),
    "new source" );
ok( $source->add_emails(
        [ { sem_email => $TEST_USERNAME . '@nosuchemail.org' } ]
    ),
    "add email address"
);
ok( $source->add_preferences( [ { sp_ptv_id => $en_US->ptv_id } ] ),
    "add en_US preferred language" );

# use load_or_save in case we aborted on earlier run and left data behind
ok( $source->load_or_save(), "save source" );

# create XML
ok( $xml = $source->as_xml( { debug => $debug, } ), "source->as_xml" );

#diag( $stxml->tidy($xml) );
like(
    $xml,
    qr(<src_status>A</src_status>),
    "xml created, source with default status"
);
like(
    $xml,
    qr(<preferred_language>en_US</preferred_language>),
    "xml has preferred_language en_US"
);

###################################
##           project
###################################

ok( my $project = AIR2Test::Project->new( prj_name => $TEST_PROJECT ),
    "new project" );
ok( $project->load_or_save, "save project" );

# create XML
ok( $xml = $project->as_xml( { debug => $debug, } ), "project->as_xml" );

#diag( $stxml->tidy($xml) );
like(
    $xml,
    qr(<prj_status>A</prj_status>),
    "xml created, project with default status"
);

###################################
##     responseset (submission)
###################################

# grab the first random one
# but only if we have some to grab
SKIP: {
    my $total = Rose::DB::Object::Manager->get_objects_count(
        'object_class' => 'AIR2::SrcResponseSet' );
    skip "No SrcResponseSets found in db", 3 if !$total;

    ok( my $srs = AIR2::SrcResponseSet->fetch_all_iterator(
            sort_by => 'srs_cre_dtim ASC'
            )->next,
        "new random SrcResponseSet"
    );

    # create XML
    ok( $xml = $srs->as_xml( { debug => $debug, } ), "srs->as_xml" );

    #diag( $stxml->tidy($xml) );
    like(
        $xml,
        qr(<prj_uuid_title>\w{12}:(.+)</prj_uuid_title>),
        "xml created, got prj_uuid_title"
    );
}

###################################
##           inquiry
###################################

SKIP: {
    my $total = Rose::DB::Object::Manager->get_objects_count(
        'object_class' => 'AIR2::Inquiry' );
    skip "No Inquirys found in db", 3 if !$total;

    ok( my $inquiry = AIR2::Inquiry->fetch_all_iterator(
            sort_by => 'inq_cre_dtim ASC'
            )->next,
        "new inquiry"
    );

    # create XML
    ok( $xml = $inquiry->as_xml( { debug => $debug, } ), "inquiry->as_xml" );

    #diag( $stxml->tidy($xml) );
    like(
        $xml,
        qr(<org_names count="\d+">),
        "xml created, inquiry with org_names"
    );
}
